<template>
  <div class="chart">
    <h3 class="title">全国</h3>
    <div class="swiper">
      <swiper ref="mySwiper" :options="swiperOptions">
        <swiper-slide v-for="(item,index) in listData" :key="index">
          <img :src="item.image" alt>
        </swiper-slide>
      </swiper>
    </div>

    <ul class="navigator">
      <li
        class="navigatorItem"
        v-for="(item,index) in listData"
        :key="index"
        :class="{active:index==currentIndex}"
        @click="clickHandle(index)"
      >{{item.title}}</li>
    </ul>
  </div>
</template>

<script>
import { Swiper, SwiperSlide, directive } from "vue-awesome-swiper";
import "swiper/swiper-bundle.css";
import axios from "axios";
export default {
  name: "MySwiper",
  components: {
    Swiper,
    SwiperSlide
  },
  directives: {
    swiper: directive
  },
  data() {
    let that = this;
    return {
      swiperOptions: {
        pagination: {
          el: ".swiper-pagination"
        },
        on: {
          slideChangeTransitionEnd: function() {
            //console.log(this.activeIndex); //切换结束时，告诉我现在是第几个slide
            that.currentIndex = this.activeIndex;
          }
        }
      },
      listData: [],
      currentIndex: 0
    };
  },
  computed: {
    swiper() {
      return this.$refs.mySwiper.$swiper;
    }
  },
  mounted() {
    //  获取轮播图数据
    axios.get(`http://iwenwiki.com/wapicovid19/ncovimg.php`).then(res => {
      // console.log(res);
      if (res.status === 200) {
        this.listData = res.data.result;
      }
    });
    //   console.log('Current Swiper instance object', this.swiper)
  },
  methods: {
    clickHandle(index) {
      this.currentIndex = index;
      this.swiper.slideTo(index, 1000, false);
    }
  }
};
</script>

<style scoped lang="less">
.chart {
  overflow: hidden;
  position: relative;
  background: #fff;
  padding: 0.16rem 0;
  .title {
    font-size: 0.16rem;
    margin: 0 0 0.08rem 0.16rem;
  }
  img {
    width: 100%;
  }
  .swiper-pagination {
    position: absolute;
    text-align: center;
    // -webkit-transition: 300ms opacity;
    // -o-transition: 300ms opacity;
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    z-index: 10;
  }
  .swiper-pagination-bullet {
    width: calc(20%-0.02rem);
    text-align: center;
    background: #f7f7f7;
    padding: 0.045rem;
    box-sizing: border-box;
    color: #666;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0.02rem;
  }
}
.navigator {
  font-size: 0.12rem;
  list-style: none;
  display: flex;
  padding: 0 0.16rem;
  justify-content: center;
  margin: 0.06rem 0 0;
}
.navigatorItem {
  color: #4169e2;
  background: #f1f5ff;
  position: relative;
  width: calc(20% - 0.02rem);
  text-align: center;
  background: #f7f7f7;
  padding: 0.045rem;
  box-sizing: border-box;
  color: #666;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 0.02rem;
  margin-left: 0.025rem;
}
.active {
  color: #4169e2;
  background: #f1f5ff;
  position: relative;
}
.swiper {
  --swiper-theme-color: #ff6600; /* 设置Swiper风格 */
  --swiper-navigation-color: #00ff00; /* 单独设置按钮颜色 */
  --swiper-navigation-size: 30px; /* 设置按钮大小 */
}
</style>
